import React, { Component } from 'react';
import storeutil from '../../util/storeutil';
import {formdateDate} from "../../util/datautil"
import menuList from "../../config/menuConfig"
import {withRouter} from "react-router-dom"
class Header extends Component {
    constructor(props) {
        super(props);
        this.state = { 
            currentTime:formdateDate(Date.now())
         }
    }
    getTime=()=>{
        this.intervalId=setInterval(()=>{
            const currentTime=formdateDate(Date.now())
            this.setState({currentTime});
        },1000)
    }
    getTitle=()=>{
        const path=this.props.location.pathname;
        let title
        menuList.forEach(item=>{
            if(item.key==path){
                title=item.title
            }else if(item.children){
              const cItem=item.children.find(cItem=>path.indexOf(cItem.key)==0)
              if(cItem){
                  title=cItem.title
              }
            }
        })
        return title
    }
    componentDidMount(){
        this.getTime()
        this.getTitle()
    }
    componentWillUnmount(){
        clearInterval(this.intervalId)
    }
    render() { 
        const {currentTime}=this.state
        const user=storeutil.getUser()
        const {username}=user
        const title=this.getTitle()
        return ( <div className="header">
            <span>欢迎----{username}</span>
            <span>当前的时间是：{currentTime}</span>
            <span>当前的位置：{title}</span>
        </div> );
    }
}
 
export default withRouter(Header);